{extend name="admin@index_layout"/}
{block name="main"}
<style type="text/css">
#change_html font {
    color: #f00;
    margin-left: 15px;
}
</style>
<div class="layui-card">
    <div class="layui-card-header">敏感词检测</div>
    <div class="layui-card-body">
        <blockquote class="layui-elem-quote">
            本功能用于检测CMS全部页面是否包含敏感词<br>违禁词库位于根目录/data/words.txt文件，一行一个<br>填写以下数据库配置代表检测其他YznCMS系统，不填则检测自身
        </blockquote>
        <form class="layui-form" method="post">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">服务器地址</label>
                    <div class="layui-input-inline">
                        <input type="text" name="hostname" autocomplete="off" placeholder="服务器地址" class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">数据库名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="database" autocomplete="off" placeholder="数据库名" class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" autocomplete="off" placeholder="数据库用户名" class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="password" autocomplete="off" placeholder="数据库密码" class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">表前缀</label>
                    <div class="layui-input-inline">
                        <input type="text" name="prefix" autocomplete="off" placeholder="数据库表前缀" class="layui-input" value="yzn_">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit>开始检测</button>
                </div>
            </div>
            <div class="layui-progress layui-progress-big" id="layui-progress-box" lay-filter="progress" lay-showPercent="yes" style="margin-top: 10px;display: none;">
                <div class="layui-progress-bar" lay-percent="0%"></div>
            </div>
            <div id="change_div" style="display: none;padding: 20px;background: #f2f2f2;margin-top: 10px;">
                <div class="scroller" style="height:250px;overflow: auto;" id="change_html"></div>
            </div>
        </form>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
layui.use(['form', 'yzn', 'layer'], function() {
    var form = layui.form,
        layer = layui.layer,
        element = layui.element,
        yzn = layui.yzn;

    form.on('submit', function(data) {
        $('.layui-btn').addClass('layui-btn-disabled').prop('disabled', true).html('<i class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i> 检测中...');
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "{:url('addons/sensitive/init', ['isadmin' => 1])}",
            data: data.field,
            success: function(res) {
                if (res.code == 1) {
                    $('#change_div').show();
                    element.progress('progress', '0%')
                    $('#layui-progress-box').show();
                    $('#change_html').html('<p>' + res.msg + '</p>');
                    start_change(0);
                } else {
                    $('.layui-btn').attr('disabled', false).removeClass("layui-btn-disabled").html('开始检测');
                    layer.msg(res.msg, { icon: 2 });
                }
            },
            error: function(xhr, textstatus, thrown) {

            }
        });
        return false;
    });

    function start_change(page) {
        $.ajax({
            type: "GET",
            dataType: "json",
            url: "{:url('addons/sensitive/start', ['isadmin' => 1])}?page=" + page,
            success: function(res) {
                $('#change_html')[0].scrollTop = $('#change_html')[0].scrollHeight;
                if (res.code == 1) {
                    if (res.data.code == -1) {
                        element.progress('progress', res.data.lun + '%')
                        start_change(res.data.page);
                        $('#change_html').append('<p>' + res.msg + '</p>');
                    } else if (res.data.code == 1) {
                        element.progress('progress', res.data.lun + '%')
                        start_change(res.data.page);
                    } else if (res.data.code == 2) {
                        $('.layui-btn').attr('disabled', false).removeClass("layui-btn-disabled").html('开始转换');
                        layer.msg(res.msg, { icon: 1 });
                    }
                } else {
                    layer.msg(res.msg, { icon: 2 });
                    $('.layui-btn').attr('disabled', false).removeClass("layui-btn-disabled").html('重新转换');
                }
            },
            error: function(xhr, textstatus, thrown) {}
        });
    }
});
</script>
{/block}